layui.use(['form','layer','table','laydate'],function(){
    $ = layui.jquery;
    var layer = layui.layer ,table = layui.table,laydate = layui.laydate,form = layui.form;
    //第一个实例
    table.render({
        elem: '#demo'
        ,id:'tabuser'
        ,height: "auto"
        ,method:"get"
        // ,async : false
        ,url: baseUrl+'room/listrooms' //数据接口
        ,where : {type:0}
        ,request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,page: true //开启分页
        ,cols: [[ //表头
            {field: 'roomNum', title: '房间号',align:'center',sort:true}
            , {field: 'state', title: '房间状态',align:'center',sort:true, templet:function (d) {
                    if(d.state == 0){
                        return "<span style='color: #1E9FFF'>未开始</span>";
                    }else{
                        return "<span style='color: #1E9FFF'>已开始</span>";
                    }
                }}
            , {field: 'gameType', title: '房间类型',align:'center',sort:true,templet:function (d) {
                    if(d.gameType == 0){
                        return "<span style='color: #1E9FFF'>推筒子</span>";
                    }else{
                        return "<span style='color: #1E9FFF'>三公</span>";
                    }
                }}

            ,{field: 'postions', title: '可入座人数',align:'center',sort:true,templet:function (d) {
                    if(d.postions != 0){
                        return "<span style='color: #1E9FFF'>"+d.postions +"人</span>";
                    }
                }}
            ,{field: 'roomGrade', title: '房间级别',align:'center',sort:true,templet:function (d) {
                    if(d.roomGrade == 0){
                        return "<span style='color: #1E9FFF'>初级场</span>";
                    }else if (d.roomGrade ==1) {
                        return "<span style='color: #1E9FFF'>中级场</span>";
                    }else if (d.roomGrade ==2) {
                        return "<span style='color: #1E9FFF'>高级场</span>";
                    }
                }}
           // ,{fixed: 'right',field: 'userType', title: '操作', width:200, align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
            ,{fixed: 'right', title: '操作', width:200, align:'center', event:"show", toolbar: '#barDemo2'} //这里的toolbar值是模板元素的选择器
        ]]
    });
    //监听工具条
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var tr = obj.tr;
        if(layEvent === 'let') { //查看下级
            var index = layui.layer.open({
                title: "房间"+data.roomName+"的详细信息",
                type: 2,
                area: ["600px","300px"],
                content: "roominfo.html",
                success: function (layero, index) {
                    var body = layui.layer.getChildFrame('body', index);
                    body.find("#roomName").val(data.roomName);
                    //console.log(data.activeTime)
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 300)
                }
            });
        }else if (layEvent=="show"){
            var lock=localStorage.getItem("lock");
            if(lock==0){
                obj.update({
                    state: "<span style='color: #1E9FFF'>未开始</span>"
                });
            }else{
                obj.update({
                    state: "<span style='color: #1E9FFF'>已开始</span>"
                });
            }
        }
    });
    var $ = layui.$, active = {
        reload: function(){
            var demoReload = $('#demoReload');
            //执行重载
            table.reload('tabuser', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    roomName: demoReload.val(),

                }
            });
        }
    };
//监听锁定操作
    form.on('switch(switchTest)',function(formObj) {
        var id = formObj.value;
        var lock = this.checked ? '1' : '0';
        var url=lock=='1'? baseUrl+"room/open":baseUrl+"room/close";
        $.ajax({
            type: 'post',
            url: url,
            data: {'id': id , "state" : lock },
            async:false,
            dataType: 'json',
            success: function(res){
                if(res.meta.code==200){
                    //加载层
                    var index = layer.load(0, {shade: false,time:500} ); //0代表加载的风格，支持0-2
                    setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:1,time:1000});index.closed}, 500);
                    localStorage.setItem("lock",lock);
                    if(lock==0){
                        layer.tips('房间已关闭', formObj.othis,{
                            tips: 1
                        });
                    }else{
                        layer.tips('房间已开启', formObj.othis,{
                            tips: 3
                        });
                    }
                }else{
                    //加载层
                    var index = layer.load(0, {shade: false,time:1000} ); //0代表加载的风格，支持0-2
                    setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:2,time:1000});index.closed}, 1000);
                }
            }
        });
    });
    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    $("button").click(function () {
        layer.open({
            type: 2
            ,area:  ['420px', '300px']
            ,shade: 0.0
            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: 'add.html'
            ,success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
               // body.find("#roomName").val(data.roomName);
                //console.log(data.activeTime)
                setTimeout(function () {
                    layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 300)
            }

        });
    })
})


